import os
from time import sleep

import allure
import page
from page.page_login import PageLogin
from page.page_wd_other import PageWdOther
from tool.get_driver import GetDriver
from tool.get_log import GetLogger

log = GetLogger.get_log()
module_name = str(os.path.basename(__file__)).split('.')[0]


@allure.epic("我的页面")
@allure.feature("分享app和更新题库测试用例")
class TestWdOther:
    @classmethod
    def setup_class(cls):
        cls.driver = GetDriver.get_app_driver()
        cls.login = PageLogin(cls.driver)
        cls.wdother = PageWdOther(cls.driver)
        try:
            cls.login.base_click_element(page.approve)
        except:
            pass
        try:
            cls.login.base_click_element(page.skip_user_desc)
        except:
            pass
        cls.login.pagelogin(os.environ['WD_OTHER_USERNAME'], password='123456')
        # cls.login.pagelogin('11100006321', password='123456')
        cls.login.base_click_element(page.wd_tab)

    @classmethod
    def teardown_class(cls):
        GetDriver.quit_app_driver()

    @allure.story("更新题库测试用例")
    def test01_wdother_update_tiku(self):
        global result
        for a in range(3):
            log.info(f"正在进行第{a + 1}次尝试")
            self.wdother.pagewdother_click_update_tiku()
            self.wdother.pagewdother_click_update_tiku()
            result = self.wdother.pagewdother_check_tiku_toast()
            if result is True:
                break
        try:
            assert result
            log.info('断言成功')
            self.wdother.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.wdother.base_get_screenshot_as_file(module_name, '更新题库失败')
            log.error('断言失败')
            raise
        finally:
            self.wdother.pagewdother_return_to_wdpage_keycode()

    @allure.story("我的页面分享APP到QQ测试用例")
    def test02_wdother_share_app_QQ(self):
        self.wdother.pagewdother_share_app_to_QQ()
        try:
            sleep(3)
            assert self.wdother.pagewdother_if_share_success()
            log.info('断言成功')
            self.wdother.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.wdother.base_get_screenshot_as_file(module_name, '分享跳转到qq失败')
            log.error('断言失败')
            raise
        finally:
            self.wdother.pagewdother_return_to_wdpage_keycode()
